import sys
from Ball import Ball
from Glass import Glass
from Constans import *
from random import randint
from pygame.sprite import collide_circle


def main():
    pygame.init()
    pygame.mixer.init()

    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption('pygame做的第一个小游戏')
    background = pygame.image.load(bg_image).convert_alpha()
    running = True

    # motion来记录鼠标在玻璃面板上产生的事件数
    motion = 0

    for i in range(5):
        position = randint(0, width - 100), randint(0, height - 100)
        print(position)
        speed = [randint(1, 10), randint(1, 10)]
        ball = Ball(gary_ball_image, green_ball_image, position, speed, bg_size, 5 * (i + 1))
        while pygame.sprite.spritecollide(ball, group, False, collide_circle):
            ball.rect.left, ball.rect.top = randint(0, width - 100), randint(0, height - 100)
        balls.append(ball)
        group.add(ball)

    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            if event.type == GAMEOVER:
                loser_sound.play()
                pygame.time.delay(2000)
                laugh_sound.play()
                running = False
            elif event.type == MOUSEMOTION:
                motion += 1
            elif event.type == MYTIMER:
                if motion:
                    for each in group:
                        if each.check(motion):
                            each.speed = [0, 0]
                            each.control = True
                    motion = 0
            elif event.type == KEYDOWN:
                if event.key == K_w:
                    for each in group:
                        if each.control:
                            each.speed[1] -= 1
                if event.key == K_s:
                    for each in group:
                        if each.control:
                            each.speed[1] += 1
                if event.key == K_a:
                    for each in group:
                        if each.control:
                            each.speed[0] -= 1
                if event.key == K_d:
                    for each in group:
                        if each.control:
                            each.speed[0] += 1
                if event.key == K_SPACE:
                    for each in group:
                        if each.control:
                            for i in hole:
                                if i[0] <= each.rect.left <= i[1] and i[2] <= each.rect.top <= i[3]:
                                    hole_sound.play()
                                    each.speed = [0, 0]
                                    group.remove(each)
                                    temp = balls.pop(balls.index(each))
                                    balls.insert(0, temp)
                                    hole.remove(i)
                            if not hole:
                                pygame.mixer.music.stop()
                                winner_sound.play()
                                pygame.time.delay(3000)
                                msg = pygame.image.load('win.png').convert_alpha()
                                msg_pos = (width - msg.get_width) // 2, (height - msg.get_height()) // 2
                                msgs.append((msg, msg_pos))
                                laugh_sound.play()
                # 临时退出
                if event.key == K_ESCAPE:
                    sys.exit()

        screen.blit(background, (0, 0))
        # 绘制面板
        area = Glass(glass_image, mouse_image, bg_size)
        screen.blit(area.glass_image, area.glass_rect)
        # 获取鼠标的当前位置，并设置替代光标的图片
        area.mouse_rect.left, area.mouse_rect.top = pygame.mouse.get_pos()
        # 限制光标的位置
        if area.mouse_rect.left < area.glass_rect.left:
            area.mouse_rect.left = area.glass_rect.left
        if area.mouse_rect.left > area.glass_rect.right - area.mouse_rect.width:
            area.mouse_rect.left = area.glass_rect.right - area.mouse_rect.width
        if area.mouse_rect.top < area.glass_rect.top:
            area.mouse_rect.top = area.glass_rect.top
        if area.mouse_rect.top > area.glass_rect.bottom - area.mouse_rect.height:
            area.mouse_rect.top = area.glass_rect.bottom - area.mouse_rect.height
        screen.blit(area.mouse_image, area.mouse_rect)

        for each in balls:
            each.move()
            if each.collide:
                each.speed = [randint(1, 10), randint(1, 10)]
                each.collide = False
            if each.control:
                screen.blit(each.green_image, each.rect)
            else:
                screen.blit(each.gray_image, each.rect)

        for each in group:
            group.remove(each)
            if pygame.sprite.spritecollide(each, group, False, collide_circle):
                each.side[0] = -each.side[0]
                each.side[1] = -each.side[1]
                each.collide = True
                if each.control:
                    each.side[0] = -1
                    each.side[1] = -1
                    each.control = False
            group.add(each)
        pygame.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()